package cn.zhangxin.newyear.system.user.service.impl;

import cn.zhangxin.newyear.constants.BaseConstants;
import cn.zhangxin.newyear.system.logininfo.domain.Logininfo;
import cn.zhangxin.newyear.system.logininfo.mapper.LogininfoMapper;
import cn.zhangxin.newyear.system.user.domain.Login;
import cn.zhangxin.newyear.system.user.domain.User;
import cn.zhangxin.newyear.system.user.mapper.UserMapper;
import cn.zhangxin.newyear.system.user.service.IUserService;
import cn.zhangxin.newyear.base.service.impl.BaseServiceImpl;
import cn.zhangxin.newyear.system.verifycode.domain.EmailCode;
import cn.zhangxin.newyear.utils.BusinessException;
import cn.zhangxin.newyear.utils.MD5Utils;
import cn.zhangxin.newyear.utils.StrUtils;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-01-30
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void emailRegister(EmailCode emailCode) {
        //邮箱校验
        if (emailCode.getEmail()==null){
            throw new BusinessException("邮箱不能为空");
        }
        //查看该邮箱是否已经被注册
        User oneByEmailUser = userMapper.findOneByEmailUser(emailCode.getEmail());
        if (oneByEmailUser!=null){
            throw new BusinessException("该邮箱已经被注册");
        }
        //校验邮箱验证码，从redis中获得邮箱验证码
        String emailCodeRedis = (String)redisTemplate.opsForValue().get(emailCode.getEmail() + BaseConstants.Redis.REGISTER);
        if (emailCodeRedis == null){
            throw new BusinessException("请先发送验证码");
        }
        //校验邮箱验证码是否正确
        if (!emailCode.getEmailCode().equals(emailCodeRedis)){
            throw new BusinessException("邮箱验证码错误");
        }
        //密码校验
        if (emailCode.getPassword()==null){
            throw new BusinessException("密码不能为空");
        }
        //二次密码校验
        if (emailCode.getPasswordRepeat()==null){
            throw new BusinessException("重复密码不能为空");
        }
        //两次密码是否相同
        if (!emailCode.getPassword().equals(emailCode.getPasswordRepeat())){
            throw new BusinessException("两次密码相同");
        }
        //补全User表数据
        User user = new User();
        //BeanUtils.copyProperties(emailCode,user);
        //补全用户名
        user.setUsername(emailCode.getEmail());
        //补全Email
        user.setEmail(emailCode.getEmail());
        //生成随机数
        String salt = StrUtils.getComplexRandomString(32);
        //补全盐值
        user.setSalt(salt);
        String passwordMD5 = MD5Utils.encrypByMd5(salt + emailCode.getPassword());
        //补全密码
        user.setPassword(passwordMD5);
        //补全状态(没绑定手机号 状态2)
        user.setState(BaseConstants.User.STATE_ACTIVATE);
        //补全注册时间
        user.setCreatetime(new Date());
        //补充logininfo表数据
        Logininfo logininfo = new Logininfo();
        //拷贝user的数据给logininfo
        BeanUtils.copyProperties(user,logininfo);
        //补全注册类型 普通用户
        logininfo.setType(BaseConstants.LogininfoType.STATE_USER);
        //补全是否可用 可用
        logininfo.setDisable(BaseConstants.LogininfoDisable.STATE_USABLE);
        //添加logininfo表 返回id设置给user
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        //user表添加
        userMapper.add(user);
    }
    /* 用户登录 */
    @Override
    public void login(Login login) {
        //用户名校验
        if (login.getUsername()==null){
            throw new BusinessException("用户名不能为空");
        }
        //密码校验
        if (login.getPassword()==null) {
            throw new BusinessException("密码不能为空");
        }
        //查询是否有该用户
        User user = userMapper.findOneByUsernameUser(login.getUsername());
        if (user == null){
            throw new BusinessException("没有该用户请先注册");
        }
        //获得盐值
        String salt = user.getSalt();
        //获得盐值和密码加密 对比
        String loginPasswordMD5 = MD5Utils.encrypByMd5(salt + login.getPassword());
        //密码和用户名校验
        if (loginPasswordMD5.equals(user.getPassword())){
            throw new BusinessException("用户名或者密码错误");
        }
    }
}
